package zl.ordermanager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zl.ordermanager.dao.DriverDao;
import zl.ordermanager.po.Tdriver;
import zl.ordermanager.service.DriverService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.Driver;
import zl.ordermanager.vo.PageHelper;

@Service
public class DriverServiceImpl implements DriverService {

	@Autowired
	private DriverDao driverDao;

	@Override
	public DataGrid<Driver> dataGrid(PageHelper pageHelper) {
		DataGrid<Driver> dataGrid = new DataGrid<Driver>();
		List<Driver> driverList = new ArrayList<Driver>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			params.put("keyword", "%%" + pageHelper.getKeywords() + "%%");
		}
		String hql = " from Tdriver t ";
		List<Tdriver> tdriverList = driverDao.find(hql + whereHql(pageHelper) + orderHql(pageHelper), params,
				pageHelper.getPageNum(), pageHelper.getNumPerPage());
		if (tdriverList != null && tdriverList.size() > 0) {
			for (Tdriver tdriver : tdriverList) {
				Driver driver = new Driver();
				BeanUtils.copyProperties(tdriver, driver);
				driverList.add(driver);
			}
		}
		dataGrid.setList(driverList);
		dataGrid.setTotal(driverDao.count("select count(*) " + hql + whereHql(pageHelper), params));
		return dataGrid;
	}

	@Override
	public DataGrid<Driver> dataGrid() {
		DataGrid<Driver> dataGrid = new DataGrid<Driver>();
		List<Driver> driverList = new ArrayList<Driver>();
		String hql = " from Tdriver t ";
		List<Tdriver> tdriverList = driverDao.find(hql);
		if (tdriverList != null && tdriverList.size() > 0) {
			for (Tdriver tdriver : tdriverList) {
				Driver driver = new Driver();
				BeanUtils.copyProperties(tdriver, driver);
				driverList.add(driver);
			}
		}
		dataGrid.setList(driverList);
		return dataGrid;
	}

	private String whereHql(PageHelper pageHelper) {
		String whereString = "";
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			whereString += " where t.driver like :keyword ";
			whereString += " or t.driverPhone like :keyword ";
		}
		return whereString;
	}

	private String orderHql(PageHelper pageHelper) {
		String orderString = "";
		if (pageHelper.getOrderField() != null && !pageHelper.getOrderField().isEmpty()
				&& pageHelper.getOrderDirection() != null && !pageHelper.getOrderDirection().isEmpty()) {
			orderString = " order by t." + pageHelper.getOrderField() + " " + pageHelper.getOrderDirection();
		}
		return orderString;
	}

	@Override
	public Driver get(Integer id) {
		Tdriver tdriver = driverDao.get(Tdriver.class, id);
		if (tdriver == null) {
			return null;
		}
		Driver driver = new Driver();
		BeanUtils.copyProperties(tdriver, driver);
		return driver;
	}

	@Override
	public void add(Driver driver) throws Exception {
		Tdriver tdriver = new Tdriver();
		BeanUtils.copyProperties(driver, tdriver);
		driverDao.save(tdriver);
	}

	@Override
	public void edit(Driver driver) throws Exception {
		Tdriver tdriver = driverDao.get(Tdriver.class, driver.getId());
		BeanUtils.copyProperties(driver, tdriver);
	}

	@Override
	public void delete(Integer id) {
		driverDao.delete(driverDao.get(Tdriver.class, id));
	}

}
